package com.animal.controller;


import com.animal.auto.JSONAuthentication;
import com.animal.entity.AnOrder;
import com.animal.mapper.AnOrderMapper;
import com.animal.response.Result;
import com.animal.service.AnOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author animal
 * @since 2022-01-28
 */
@RestController
@RequestMapping("/an-order")
public class AnOrderController extends JSONAuthentication {
    @Autowired
    private AnOrderMapper anOrderMapper;
    @Autowired
    private AnOrderService anOrderService;

    @ApiOperation(value = "添加订单")
    @PostMapping("/add")
    public void addOrder(HttpServletRequest request, HttpServletResponse response, @RequestBody AnOrder anOrder) throws  Exception{
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        anOrder.setOrCreatTime(timestamp);

        int i=anOrderMapper.insert(anOrder);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除订单")
    @PostMapping("/del")
    public void delOrder(HttpServletRequest request, HttpServletResponse response, @RequestParam("orId") int orId ) throws Exception{
        int i=anOrderMapper.deleteById(orId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "修改订单")
    @PostMapping("/upd")
    public void updAnimal(HttpServletRequest request, HttpServletResponse response, @RequestBody AnOrder anOrder)throws Exception{
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        anOrder.setOrUpdTime(timestamp);

        int i=anOrderMapper.updateById(anOrder);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询订单")
    @PostMapping("/look")

    public void lookOrder(HttpServletRequest request, HttpServletResponse response,@RequestParam("current") int current,@RequestParam("size") int size)throws  Exception{

        int total=anOrderMapper.selectCount(null);

        List<AnOrder> list =anOrderService.findAll(size,current);
        System.out.println(list.size());
        Result ok = Result.ok().data("order",list);
        ok.data("total",total);
        this.WriteJSON(request, response, ok);
    }
    @ApiOperation(value = "查询订单通过用户id")
    @PostMapping("/lookByName")
    public void findorderByUserId(HttpServletRequest request, HttpServletResponse response,@RequestParam("current") int current,@RequestParam("size") int size,@RequestParam("userId") int userId) throws  Exception{
        QueryWrapper<AnOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        int total=anOrderMapper.selectCount(wrapper);
        List<AnOrder> list =anOrderService.findorderByUserId(current,size,userId);

        Result ok = Result.ok().data("order",list);
        ok.data("total",total);
        this.WriteJSON(request, response, ok);
    }

    @ApiOperation(value = "修改订单状态通过订单id")
    @PostMapping("/updOrderByid")
    public void updOrderByid(HttpServletRequest request, HttpServletResponse response,@RequestParam("orId") int orId,@RequestParam("orderStatus") int orderStatus) throws  Exception{
        AnOrder anOrder=anOrderMapper.selectById(orId);
        anOrder.setOrderStatus(orderStatus);
       int n=anOrderMapper.updateById(anOrder);
        if(n>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

}

